{extend name='bus@public/wrap'}
{block name="main"}
<div class="log-block">
    <div id="up">
        <input type="file" value="Upload" name="upload">
    </div>
    <table id="table" style="margin-top:10px;"></table>
    <input type="hidden" name="is_look" value="{$Think.get.is_look}">
    <input type="hidden" name="rel" value="{$Think.get.rel}">
    <input type="hidden" name="key" value="{$Think.get.key}">
</div>
<script>
    $(function(){
        if($("[name='is_look']").val() == "yes"){
            $("[name='upload']").hide();
        }
    });
    $('#table').bootstrapTable({
        url:'/bus/upload/lst',
        pagination:false,
        columns: [
            { field: 'file_name', title: '文件名' },
            { field: 'file_size', title: 'Size',formatter:func.fileSize },
            { field: 'file_type', title: 'Type' },
            { field: 'file_ext', title: 'Ext' },
            { field: '', title: '下载', formatter:function(valur,row,index){
                var html = '';
                html= "<a href='" + row.file_url + "' target='_blank'>下载</a>";
                html += ' ';
                html += "<a data-del='" + row.id + "' target='_blank'>删除</a>";
                return html;
            } },
        ],
        queryParams: function (params) {
            var rel = $('[name=rel]').val();
            if(!rel){
                rel = func.guid();
            }
            params.rel = rel;
            return params;
        },onLoadSuccess: function (res) {
            if($("[name='is_look']").val() == "yes"){
                $("[data-del]").hide();
            }
        }
    });
    function bind() {
        $("#table").bootstrapTable("refresh", { silent: false });
    }
    //上传文件
    $("#up").on("change","[name=upload]",function(){
        var rel = $('[name=rel]').val();
        if(!rel){
            rel = func.guid();
        }
        var key = $('[name="key"]').val();
        var fileObj = $(this)[0];
        var index = layer.load(0, { shade:0.2 });
        for(var i=0;i<fileObj.files.length;i++){
            var file = fileObj.files[i];
            var form = new FormData();
            form.append("file", file);
            form.append("rel", rel);
            form.append("key", key);
            var xhr = new XMLHttpRequest();
            xhr.open("post", "/api/upload/upfile", true);
            xhr.onload = function (e) {
                var html = e.currentTarget.responseText;
                var obj = JSON.parse(html);
                if(obj.code == 0) {
                    console.log(obj);
                    bind();
                } else {
                    layer.msg(obj.text, {icon: 5});
                }
            };
            xhr.onloadend = function (e) {
                layer.close(index);
            }
            xhr.send(form);
        }
        $(this).remove();
        $("#up").append('<input type="file" value="Upload" name="upload">');
    });
    $("[name=upload000]").on("click",function(){
        var rel = func.urlParam('rel');
        //创建页面元素
        var fileObj = document.createElement("input");
        fileObj.setAttribute("type", "file");
        $(fileObj).change(function () {
            var index = layer.load(0, { shade:0.2 });
            for(var i=0;i<fileObj.files.length;i++){
                var file = fileObj.files[i];

                var form = new FormData();
                form.append("file", file);
                form.append("rel", rel);
                var xhr = new XMLHttpRequest();
                xhr.open("post", "/api/upload/upfile", true);
                xhr.onload = function (e) {
                    var html = e.currentTarget.responseText;
                    var obj = JSON.parse(html);
                    if(obj.code == 0) {
                        console.log(obj);
                        bind();
                    } else {
                        layer.msg(obj.text, {icon: 5});
                    }
                };
                xhr.onloadend = function (e) {
                    layer.close(index);
                }
                xhr.send(form);
            }
        });
        document.appendChild(fileObj);
        fileObj.click();
        $(fileObj).trigger("click");
    });
    //删除
    $("#table").on('click', '[data-del]',function(){
        var id = $(this).data('del');
        var lindex = layer.load(2 ,{ icon: 2 ,shade: 0.1 });
        //提交数据
        frame.ajax({
            url:"/api/upload/delfile",
            type:"post",
            data:{id:id}
        }).then(function(data){
            frame.show(data);
            if(data.code == 0){
                bind();
            }
        });
    });
</script>
{/block}